home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9204 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: ix.netcom.com!netnews
  2. From: joebelle@ix.netcom.com (Al Belle)
  3. Newsgroups: comp.lang.c
  4. Subject: Need help with sorting and building tree
  5. Date: Sat, 09 Mar 1996 06:03:22 GMT
  6. Organization: Netcom
  7. Message-ID: <4hpti1$d5t@ixnews2.ix.netcom.com>
  8. NNTP-Posting-Host: ix-ytn-oh1-19.ix.netcom.com
  9. X-NETCOM-Date: Fri Mar 08 10:16:33 AM PST 1996
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12.  
  13.                     Here is my situation.
  14. I have an array of string that I must sort using the binary sort method and while I am sorting must recursivly
  15. put them into a binary tree. I have tried everything and am running out of time. My array is defined globally.
  16.  
  17.  
  18.               void sort(nodetype *p,int high,int low)
  19.                     {
  20.                       int middle=0;
  21.                      while(low<=high)
  22.                        {
  23.                          middle=(low+high)/2;
  24.                           maketree(p,array[middle]);
  25.                           high=middle-1;
  26.                         }
  27.            }
  28.  
  29.       void maketree(nodetype *p, char *x)
  30.        {
  31.          if (p==NULL)
  32.            {
  33.             p=(nodetype *)malloc(sizeof(nodetype));
  34.                if(p!=NULL){
  35.                   p->info=x;
  36.                   p->left=p->right=NULL;}
  37.                else
  38.                  printf("No memory\n");
  39.           }
  40.       else
  41.           if (x < p->info) <-------------------------------------------------------This doesnt seem to work either????????
  42.              maketree(p->left,x)
  43.          else
  44.             if (x> p->info)
  45.              maketree(p->right,x)
  46. }
  47.  
  48. Am I comparing the value to be inserted "(x<p->info)" right. I figured the compiler would use the intiger values if I
  49. tried to compare them.(Im using GCC) Only 5 items of my array are being put into the tree out of a possible 52
  50. (The 52 states!);  I know my logic is wrong somewhere but cant seem to figure it out. If anyone could post a 
  51. possible suggestion It would be great.
  52.                                             TIA
  53.                                                       joebelle@ix.netcom.com
  54.  
  55.  
  56.  
  57.